package com.llkj.contractmanagement.customview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;


import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.pickerview.widget.OnWheelScrollListener;
import com.llkj.contractmanagement.customview.pickerview.widget.WheelView;
import com.llkj.contractmanagement.customview.pickerview.widget.adapters.ArrayWheelAdapter;

import java.util.ArrayList;

/**
 * 选项选择器，可支持一二三级联动选择
 *
 * @author Sai
 */
public class OptionsPopupWindow extends PopupWindow implements OnClickListener, OnWheelScrollListener {
    private View rootView; // 总的布局
    private View btnSubmit, btnCancel;
    private OptionWheelViewListener optionWheelViewListener;
    private ArrayList<String> first_list, second_list, third_list;
    private WheelView wv_first, wv_second, wv_third;
    private Context context;

    public OptionsPopupWindow(Context context) {
        super(context);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new BitmapDrawable());// 这样设置才能点击屏幕外dismiss窗口
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.timepopwindow_anim_style);
        this.context = context;
        initView();
        setContentView(rootView);
    }

    private void initView() {
        rootView = LayoutInflater.from(context).inflate(R.layout.pw_options, null);
        btnSubmit = rootView.findViewById(R.id.btnSubmit);
        btnCancel = rootView.findViewById(R.id.btnCancel);
        wv_first = (WheelView) rootView.findViewById(R.id.options1);
        wv_second = (WheelView) rootView.findViewById(R.id.options2);
        wv_third = (WheelView) rootView.findViewById(R.id.options3);
        setListener();
    }

    private void setListener() {
        btnCancel.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
        wv_first.addScrollingListener(this);
        wv_second.addScrollingListener(this);
        wv_third.addScrollingListener(this);
    }

    public void setOptionOneAdapter(ArrayList<String> provinces) {
        this.first_list = provinces;
        wv_first.setViewAdapter(new ArrayWheelAdapter(context, provinces));// 设置显示数据
        wv_first.setCurrentItem(0);
    }

    public void setOptionTwoAdapter(ArrayList<String> cities) {
        this.second_list = cities;
        wv_second.setViewAdapter(new ArrayWheelAdapter(context, cities));// 设置显示数据
        if (cities.size() > 0) {
            wv_second.setCurrentItem(0);
        }
    }

    public void setOptionThreeAdapter(ArrayList<String> districts) {
        this.third_list = districts;
        wv_third.setViewAdapter(new ArrayWheelAdapter(context, districts));// 设置显示数据
        if (districts.size() > 0) {
            wv_third.setCurrentItem(0);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnCancel) {
        } else if (v.getId() == R.id.btnSubmit) {
            if (optionWheelViewListener != null) {
                optionWheelViewListener.onOptionsSelect(wv_first.getCurrentItem(), wv_second.getCurrentItem(), wv_third.getCurrentItem());
            }
        }
        dismiss();
    }


    @Override
    public void onScrollingStarted(WheelView wheel) {

    }

    @Override
    public void onScrollingFinished(WheelView wheel) {
        if (wheel == wv_first) {
            optionWheelViewListener.onChangeListener(0, wheel.getCurrentItem());
        } else if (wheel == wv_second) {
            optionWheelViewListener.onChangeListener(1, wheel.getCurrentItem());
        }
    }

    public interface OptionWheelViewListener {
        void onOptionsSelect(int options1, int option2, int options3);

        void onChangeListener(int type, int option);
    }

    public void setOptionWheelViewListener(
            OptionWheelViewListener optionWheelViewListener) {
        this.optionWheelViewListener = optionWheelViewListener;
    }

    /**
     * 设置wheel是否显示
     */
    public void setVisible(boolean first, boolean second, boolean third) {
        if (wv_first != null)
            if (!first)
                wv_first.setVisibility(View.GONE);

        if (wv_second != null)
            if (!second)
                wv_second.setVisibility(View.GONE);

        if (wv_third != null)
            if (!third)
                wv_third.setVisibility(View.GONE);
    }
}
